package FX;

import BasicClass.Book;
import BasicClass.User;
import Connection.AccountDaoImpl;
import Connection.BookDaoImpl;
import Connection.MyConnection;
import DAO.DaoException;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ChangeUI {
    public void display(Stage stage, int choose, Object object){
        VBox rootNode = new VBox();
        HBox Control = new HBox();
        Button ControlConfirm = new Button("确认");
        Button ControlQuit = new Button("取消");
        Control.setAlignment(Pos.CENTER);
        Control.setSpacing(30);
        Control.getChildren().addAll(ControlConfirm, ControlQuit);
        VBox vBox = new VBox();

        HBox BookName = new HBox();
        Label BookNameLab = new Label("书名");
        TextField BookNameText = new TextField();
        BookNameText.setPrefWidth(150);
        BookName.getChildren().addAll(BookNameLab, BookNameText);

        HBox Author = new HBox();
        Label AuthorLab = new Label("作者");
        TextField AuthorText = new TextField();
        AuthorText.setPrefWidth(100);
        Author.getChildren().addAll(AuthorLab, AuthorText);

        HBox Press = new HBox();
        Label PressLab = new Label("出版社");
        TextField PressText = new TextField();
        PressText.setPrefWidth(200);
        Press.getChildren().addAll(PressLab, PressText);

        HBox Price = new HBox();
        Label PriceLab = new Label("价格");
        TextField PriceText = new TextField();
        PriceText.setPrefWidth(100);
        Price.getChildren().addAll(PriceLab, PriceText);

        HBox Stock = new HBox();
        Label StockLab = new Label("余存量");
        TextField StockText = new TextField();
        StockText.setPrefWidth(100);
        Stock.getChildren().addAll(StockLab, StockText);

        HBox Location = new HBox();
        Label LocationLab = new Label("位置");
        TextField LocationText = new TextField();
        LocationText.setPrefWidth(200);
        Location.getChildren().addAll(LocationLab, LocationText);

        HBox BookLabel = new HBox();
        Label BookLabelLab = new Label("类别");
        TextField BookLabelText = new TextField();
        BookLabelText.setPrefWidth(300);
        BookLabel.getChildren().addAll(BookLabelLab, BookLabelText);

        HBox Remark = new HBox();
        Label RemarkLab = new Label("备注");
        TextField RemarkText = new TextField();
        RemarkText.setPrefWidth(200);
        Remark.getChildren().addAll(RemarkLab, RemarkText);

        HBox AccountNumber = new HBox();
        Label AccountNumberLab = new Label("账户");
        TextField AccountNumberText = new TextField();
        AccountNumberText.setPrefWidth(150);
        AccountNumber.getChildren().addAll(AccountNumberLab, AccountNumberText);

        HBox AccountName = new HBox();
        Label AccountNameLab = new Label("姓名");
        TextField AccountNameText = new TextField();
        AccountNameText.setPrefWidth(150);
        AccountName.getChildren().addAll(AccountNameLab, AccountNameText);

        HBox AccountPassword = new HBox();
        Label AccountPasswordLab = new Label("密码");
        TextField AccountPasswordText = new TextField();
        AccountPasswordText.setPrefWidth(150);
        AccountPassword.getChildren().addAll(AccountPasswordLab, AccountPasswordText);

        HBox Sex = new HBox();
        Label SexLab = new Label("性别");
        TextField SexText = new TextField();
        SexText.setPrefWidth(150);
        Sex.getChildren().addAll(SexLab, SexText);

        HBox aClass = new HBox();
        Label aClassLab = new Label("班级");
        TextField aClassText = new TextField();
        aClassText.setPrefWidth(150);
        aClass.getChildren().addAll(aClassLab, aClassText);

        HBox Title = new HBox();
        Label TitleLab = new Label("职称");
        TextField TitleText = new TextField();
        TitleText.setPrefWidth(100);
        Title.getChildren().addAll(TitleLab, TitleText);

        HBox Faction = new HBox();
        Label FactionLab = new Label("系别");
        TextField FactionText = new TextField();
        FactionText.setPrefWidth(200);
        Faction.getChildren().addAll(FactionLab, FactionText);

        HBox Grade = new HBox();
        Label GradeLab = new Label("权限");
        TextField GradeText = new TextField();
        GradeText.setPrefWidth(150);
        Grade.getChildren().addAll(GradeLab, GradeText);

        HBox Line1 = new HBox();
        HBox Line2 = new HBox();

        MyConnection connection = ManagerUI.getMyConnection();

        if(choose==1){
            LayoutChoose(rootNode, Control, vBox, BookName, Author, Press, Price, Stock, Location, BookLabel, Remark, Line1, Line2);
            Scene scene = new Scene(rootNode, 900, 120);
            stage.setTitle("修改书籍");
            stage.setScene(scene);
            stage.setResizable(false);
            stage.getIcons().add(new Image("./fodder/控制台.png"));
            stage.show();
            ControlConfirm.setOnMouseClicked(e->{
                Book b = (Book) object;
                if(BookNameText.getText()!=null&!BookNameText.getText().equals("")) b.setName(BookNameText.getText());
                if(AuthorText.getText()!=null&!AuthorText.getText().equals("")) b.setAuthor(AuthorText.getText());
                if(PressText.getText()!=null&!PressText.getText().equals("")) b.setPress(PressText.getText());
                if(PriceText.getText()!=null&!PriceText.getText().equals("")) b.setPrice(Double.valueOf(PriceText.getText()));
                if(StockText.getText()!=null&!StockText.getText().equals("")) b.setStock(Integer.parseInt(StockText.getText()));
                if(LocationText.getText()!=null&!LocationText.getText().equals("")) b.setLocation(LocationText.getText());
                if(BookLabelText.getText()!=null&!BookLabelText.getText().equals("")) b.setLabel(BookLabelText.getText());
                if(RemarkText.getText()!=null&!RemarkText.getText().equals("")) b.setRemark(RemarkText.getText());
                BookDaoImpl bookDao = new BookDaoImpl(connection);
                try {
                    bookDao.updateBook(b);
                } catch (DaoException ex) {
                    throw new RuntimeException(ex);
                }
                stage.hide();
            });
        }
        else if(choose==2){
            Line1.getChildren().addAll(AccountName, AccountPassword, Sex, aClass);
            Line2.getChildren().addAll(Title, Faction, Grade);
            vBox.getChildren().addAll(Line1, Line2);
            vBox.setSpacing(10);
            rootNode.getChildren().addAll(vBox, Control);
            rootNode.setSpacing(10);rootNode.setPadding(new Insets(10));
            Scene scene = new Scene(rootNode, 900, 120);
            stage.setTitle("修改账户");
            stage.setScene(scene);
            stage.setResizable(false);
            stage.getIcons().add(new Image("./fodder/控制台.png"));
            stage.show();
            ControlConfirm.setOnMouseClicked(e->{
                TextGetCon(stage, (User) object, AccountNameText, AccountPasswordText,
                        SexText, aClassText, TitleText, FactionLab, FactionText, GradeText, connection);
            });
        }
        else if (choose==3) {
            LayoutChoose(rootNode, Control, vBox, AccountNumber, AccountName,
                    AccountPassword, Sex, aClass, Title, Faction, Grade, Line1, Line2);
            Scene scene = new Scene(rootNode, 950, 120);
            stage.setTitle("添加账户");
            stage.setScene(scene);
            stage.setResizable(false);
            stage.getIcons().add(new Image("./fodder/控制台.png"));
            stage.show();
            ControlConfirm.setOnMouseClicked(e->{
                if(object!=null) {
                    TextGetCon(stage, (User) object, AccountNameText, AccountPasswordText,
                            SexText, aClassText, TitleText, FactionLab, FactionText, GradeText, connection);
                }
            });
        }
        ControlQuit.setOnMouseClicked(e->stage.hide());
    }

    private void TextGetCon(
            Stage stage, User object, TextField accountNameText, TextField accountPasswordText,
            TextField sexText, TextField aClassText, TextField titleText, Label factionLab,
            TextField factionText, TextField gradeText, MyConnection connection) {
        if(accountNameText.getText()!=null&!accountNameText.getText().equals("")) object.setName(accountNameText.getText());
        if(accountPasswordText.getText()!=null&!accountPasswordText.getText().equals("")) object.setPassword(accountPasswordText.getText());
        if(sexText.getText()!=null&!sexText.getText().equals("")) object.setSexy(sexText.getText());
        if(aClassText.getText()!=null&!aClassText.getText().equals("")) object.setDiscipline(aClassText.getText());
        if(titleText.getText()!=null&!titleText.getText().equals("")) object.setTitle(titleText.getText());
        if(factionText.getText()!=null&!factionText.getText().equals("")) object.setFaction(factionLab.getText());
        if(gradeText.getText()!=null&!gradeText.getText().equals("")) object.setGrade(gradeText.getText());
        AccountDaoImpl accountDao = new AccountDaoImpl(connection);
        try{
            accountDao.updateAccount(object, 2);
        } catch (DaoException ex) {
            throw new RuntimeException(ex);
        }
        stage.hide();
    }

    private void LayoutChoose(
            VBox rootNode, HBox control, VBox vBox, HBox bookName, HBox author, HBox press,
            HBox price, HBox stock, HBox location, HBox bookLabel, HBox remark, HBox line1, HBox line2) {
        line1.getChildren().addAll(bookName, author, press, price, stock);
        line2.getChildren().addAll(location, bookLabel, remark);
        vBox.getChildren().addAll(line1, line2);
        vBox.setSpacing(10);
        rootNode.getChildren().addAll(vBox, control);
        rootNode.setSpacing(10);
        rootNode.setPadding(new Insets(10));
    }
}
